/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poo.biblioteca.vistas.generos;

import biblioteca.entidades.Genero;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.JOptionPane;
import poo.biblioteca.vistas.IfrBase;

/**
 *
 * @author apo
 */
public class IfrMnttoGenero extends IfrBase {

    /**
     * Creates new form IfrMnttoGenero
     */
    public IfrMnttoGenero() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlBusqueda = new javax.swing.JPanel();
        lblIngreseNombre = new javax.swing.JLabel();
        txtNombreGenero = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        scrMnttoGenero = new javax.swing.JScrollPane();
        tblGeneros = new javax.swing.JTable();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Mantenimiento de Géneros");

        pnlBusqueda.setBorder(javax.swing.BorderFactory.createTitledBorder("Búsqueda"));

        lblIngreseNombre.setText("Ingrese nombre:");

        txtNombreGenero.setToolTipText("Escriba el nombre del Género o parte de él");

        btnBuscar.setMnemonic('B');
        btnBuscar.setText("Buscar");

        javax.swing.GroupLayout pnlBusquedaLayout = new javax.swing.GroupLayout(pnlBusqueda);
        pnlBusqueda.setLayout(pnlBusquedaLayout);
        pnlBusquedaLayout.setHorizontalGroup(
            pnlBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBusquedaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblIngreseNombre)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtNombreGenero)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnBuscar)
                .addContainerGap())
        );
        pnlBusquedaLayout.setVerticalGroup(
            pnlBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBusquedaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblIngreseNombre)
                    .addComponent(txtNombreGenero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBuscar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        scrMnttoGenero.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultados"));

        tblGeneros.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblGeneros.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        scrMnttoGenero.setViewportView(tblGeneros);

        btnModificar.setMnemonic('M');
        btnModificar.setText("Modificar");
        btnModificar.setEnabled(false);

        btnEliminar.setMnemonic('E');
        btnEliminar.setText("Eliminar");
        btnEliminar.setEnabled(false);

        btnCancelar.setMnemonic('C');
        btnCancelar.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrMnttoGenero, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
                    .addComponent(pnlBusqueda, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnModificar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrMnttoGenero, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnModificar)
                    .addComponent(btnEliminar)
                    .addComponent(btnCancelar))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JLabel lblIngreseNombre;
    private javax.swing.JPanel pnlBusqueda;
    private javax.swing.JScrollPane scrMnttoGenero;
    private javax.swing.JTable tblGeneros;
    private javax.swing.JTextField txtNombreGenero;
    // End of variables declaration//GEN-END:variables

    public void btnBuscarAddActionListener(ActionListener actionListener) {
        btnBuscar.addActionListener(actionListener);
    }

    public String getNombreGenero() {
        return txtNombreGenero.getText();
    }

    public void setTableModel(TbmGenero tableModel) {
        tblGeneros.setModel(tableModel);
    }

    public TbmGenero getTableModel() {
        return (TbmGenero) tblGeneros.getModel();
    }

    public void btnCancelarAddActionListener(ActionListener actionListener) {
        btnCancelar.addActionListener(actionListener);
    }

    public void tblGenerosAddMouseListener(MouseListener mouseListener) {
        tblGeneros.addMouseListener(mouseListener);
    }

    public int getFilaSeleccionada() {
        return tblGeneros.getSelectedRow();
    }

    public void permitirEdicion(boolean permitir) {
        btnModificar.setEnabled(permitir);
        btnEliminar.setEnabled(permitir);
    }

    public void btnEliminarAddActionListener(ActionListener actionListener) {
        btnEliminar.addActionListener(actionListener);
    }

    public Genero getGeneroSeleccionado() {
        int filaSeleccionada = tblGeneros.getSelectedRow();
        if (filaSeleccionada < 0) {
            return null;
        }
        Genero generoSeleccionado = ((TbmGenero) tblGeneros.getModel())
                .getGeneroEnFila(filaSeleccionada);

        return generoSeleccionado;
    }

    public void eliminarGeneroSeleccionado() {
        getTableModel().eliminar(tblGeneros.getSelectedRow());
    }

    public boolean confirmarEliminacion() {
        return (JOptionPane.showConfirmDialog(this, "¿Está seguro de eliminar "
                + "el Género seleccionado?", "Confirmación", 
                JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION ? 
                true:false);
    }

    public void deseleccionarTabla() {
        tblGeneros.clearSelection();
        permitirEdicion(false);
    }

    public void btnModificarAddActionListener(ActionListener actionListener) {
        btnModificar.addActionListener(actionListener);
    }

    public boolean confirmarModificacion() {
         return (JOptionPane.showConfirmDialog(this, "¿Está seguro de modificar "
                + "el Género seleccionado?", "Confirmación", 
                JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION ? 
                true:false);
    }

    public void modificarGeneroSeleccionado(String nuevoNombreGenero) {
        getTableModel().modificar(tblGeneros.getSelectedRow(), nuevoNombreGenero);
    }
}
